package shape;


import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import constant.Constant;
import entry.Position;


import java.util.HashMap;
import java.util.Map;


public class QiMaDingShape implements Shape {
    float pt = 72f / 25.4f;
    private Position position;
    private int pageCount;
    private int rotate;
    private float w;
    private float h;
    private int mIndex;
    private float cx;
    private float jj;
    private float space;
    private float jx;
    private int pos;



    public QiMaDingShape(Position position, int pageCount, int rotate, float w, float h, int mIndex, float cx, float space, float jj, float jx, int pos) {
        this.position = position;
        this.pageCount = pageCount;
        this.rotate = rotate;
        this.w = w;
        this.h = h;
        this.mIndex = mIndex;
        this.cx = cx;
        this.space = space;
        this.jj = jj;
        this.jx = jx;
        this.pos = pos;

    }


    @Override
    public void draw(PdfContentByte cb) {

    }

    @Override
    public void draw(PdfContentByte cb, PdfWriter writer) {
        try {
            PdfReader reader = new PdfReader(Constant.pdf_path);
            if(pageCount==15||pageCount==2||pageCount==3||pageCount==14
                    ||pageCount==17 ||pageCount==32||pageCount==29 ||pageCount==20
                    ){
//                System.out.println(123);
            }
            System.out.println("mindex:"+mIndex+",pos:"+pos+",page:"+pageCount+",rotate:"+rotate);
            Constant.k++;
            Constant.pdfReaderMap.put(Constant.k,reader);
//            importedPage.setBoundingBox(new Rectangle(mask*Constant.pt,importedPage.getHeight()-mask*Constant.pt,importedPage.getWidth()    -mask*Constant.pt,mask*Constant.pt));
            if (this.rotate == 0) {
                PdfImportedPage importedPage = writer.getImportedPage(reader, pageCount);
                importedPage.setBoundingBox(getRectangle(this.mIndex));
                cb.addTemplate(importedPage, (position.getX() - cx + jj + jx) * Constant.pt, (position.getY() - cx + jj + jx) * Constant.pt);
            } else if (this.rotate == 90) {
                PdfImportedPage importedPage = writer.getImportedPage(reader, pageCount);
                cb.addTemplate(importedPage, 0, 1, -1, 0, position.getX() * Constant.pt + importedPage.getHeight(), position.getY() * Constant.pt);
//                importedPage.setBoundingBox(new Rectangle(0,0,importedPage.getHeight()+mask*pt/2,importedPage.getWidth()+mask*pt/2));
            } else if (this.rotate == 270) {
                PdfImportedPage importedPage = writer.getImportedPage(reader, pageCount);
                cb.addTemplate(importedPage, 0, -1, 1, 0, position.getX() * Constant.pt, position.getY() * Constant.pt + importedPage.getWidth());
//                importedPage.setBoundingBox(new Rectangle(mask*pt,mask*pt,importedPage.getHeight(),importedPage.getWidth()));
            } else if (this.rotate == 180) {
                PdfImportedPage importedPage = writer.getImportedPage(reader, pageCount);
                importedPage.setBoundingBox(getRectangle(this.mIndex));
                cb.addTemplate(importedPage, -1, 0, 0, -1, (position.getX() + jj + jx + cx + w) * Constant.pt, (position.getY() + jj + jx + cx + h) * Constant.pt);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Rectangle getRectangle(int mIndex) {

        float c = cx * pt;
        float s = space * pt;
        float j = jj * pt;
        Rectangle rectangle = new Rectangle(0, 0, w * pt + c + s / 2, h * pt + c + s / 2);
        if (this.rotate == 0) {
            rectangle = getRectangle0(mIndex, c, s, j, rectangle);
        } else if (this.rotate == 180) {

            rectangle = getRectangle180(mIndex, c, s, j, rectangle);
        }
        return rectangle;
    }

    private Rectangle getRectangle180(int mIndex, float c, float s, float j, Rectangle rectangle) {
        switch (mIndex) {
            case 1:
                if (pos == 4) {
                    rectangle = new Rectangle(c - j, c - j, w * pt + c, h * pt + c + s / 2);
                } else if (pos == 1) {
                    rectangle = new Rectangle(c, c - j, w * pt + c + j, h * pt + c + s / 2);
                } else {
                    rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + s / 2);
                }
                break;
            case 4:
                if (pos == 2) {
                    rectangle = new Rectangle(c - s / 2, c - j, w * pt + c, h * pt + c + s / 2);
                } else if (pos == 3) {
                    rectangle = new Rectangle(c, c - j, w * pt + c + s / 2, h * pt + c + s / 2);
                } else {
//                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + s / 2);
//                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c, h * pt + c + s / 2);
                    rectangle = new Rectangle(c, c - j, w * pt + c + s / 2, h * pt + c + s / 2);
                }

                break; //可选
            case 7:
                if (pos == 1) {
                    rectangle = new Rectangle(c, c - j, w * pt + c + j, h * pt + c + s / 2);
                } else if (pos == 4) {
                    rectangle = new Rectangle(c - j, c - j, w * pt + c, h * pt + c + s / 2);
                } else {
                    rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + s / 2);
                }

                break; //可选
            case 2:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(j,s/2,0,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(0,s/2,j,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break;
            case 5:
                switch (pos) {
                    case 2:
                        rectangle = getRectangle2(0,s/2,s/2,s/2);
                        break;
                    case 3:
                        rectangle = getRectangle2(s/2,s/2,0,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break; //可选
            case 8:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(j,s/2,0,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(0,s/2,j,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break; //可选
            case 3:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(j,j,0,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(0,j,j,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break; //可选
            case 6:
                switch (pos) {
                    case 3:
                        rectangle = getRectangle2(s/2,j,0,s/2);
                        break;
                    case 2:
                        rectangle = getRectangle2(0,j,s/2,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break; //可选
            case 9:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(j,j,0,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(0,j,j,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break; //可选
            case 10:
                rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + j);
                break; //可选
            case 11:
                rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + j);
                break; //可选
            case 12:
                rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + j);
                break; //可选
            case 13:
                rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                break; //可选
            case 14:
                rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                break; //可选
            case 15:
                rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                break; //可选
            case 16:
                rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                break; //可选
        }
        return rectangle;
    }

    private Rectangle getRectangle0(int mIndex, float c, float s, float j, Rectangle rectangle) {
        switch (mIndex) {
            case 1:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,j,j,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,j,0,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 4:
                switch (pos) {
                    case 2:
                        rectangle = getRectangle2(s/2,j,0,s/2);
                        break;
                    case 3:
                        rectangle = getRectangle2(0,j,s/2,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 7:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,j,j,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,j,0,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 2:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,s/2,j,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,s/2,0,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break;
            case 5:

                switch (pos) {
                    case 2:
                        rectangle = getRectangle2(s/2,s/2,0,s/2);
                        break;
                    case 3:
                        rectangle = getRectangle2(0,s/2,s/2,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }



                break; //可选
            case 8:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,s/2,j,s/2);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,s/2,0,s/2);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }

                break; //可选
            case 3:
                if (pos == 1) {

                    rectangle = new Rectangle(c - j, c - s / 2, w * pt + c, h * pt + c + j);
                } else if (pos == 4) {

                    rectangle = new Rectangle(c, c - s / 2, w * pt + c + j, h * pt + c + j);
                } else {

                    rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                }
                break;
            case 6:
                if (pos == 2) {
                    rectangle = new Rectangle(c, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                } else if (pos == 3) {
                    rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c, h * pt + c + j);
                } else {
                    rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                }
                break; //可选
            case 9:
                if (pos == 1) {
                    rectangle = new Rectangle(c - j, c - s / 2, w * pt + c, h * pt + c + j);
                } else if (pos == 4) {
                    rectangle = new Rectangle(c, c - s / 2, w * pt + c + j, h * pt + c + j);
                } else {
                    rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + j, h * pt + c + j);
                }
                break; //可选
            case 10:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,j,j,j);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,j,0,j);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 11:
                switch (pos) {
                    case 2:
                        rectangle = getRectangle2(s/2,j,0,j);
                        break;
                    case 3:
                        rectangle = getRectangle2(0,j,s/2,j);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 12:
                switch (pos) {
                    case 4:
                        rectangle = getRectangle2(0,j,j,j);
                        break;
                    case 1:
                        rectangle = getRectangle2(j,j,0,j);
                        break;
                    default:
                        rectangle = getRectangle2(0,0,0,0);
                        break;
                }
                break;
            case 13:
                rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                break; //可选
            case 14:
                rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                break; //可选
            case 15:
                rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                break; //可选
            case 16:
                rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                break; //可选
        }
        return rectangle;
    }

    private Rectangle getRectangle2(float x1, float y1, float x2, float y2) {
        return new Rectangle(cx*pt - x1, cx*pt - y1, w * pt+cx*pt + x2, h * pt+cx*pt + y2);
    }


}
